<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 8/16/14
 * Time: 7:18 PM
 */

class Location extends MY_Controller {
    function index() {
        $data = array("pageTitle" => "Location Management");
        $this->load->view("admin/branch", $data);
    }

    function getLocation($id = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            if ($id == null) {
                $branches = $this->findAll("Branch");
                $response->setData($branches);
            } else if ($id != "") {
                $branch = $this->findById("Branch", $id);
                $response->setData($branch);
            } else {
                throw new Exception("Invalid Branch Id " . $id);
            }
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response);
    }

    function addEditLocation() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            $id = trim($this->input->post("location-id"));
            $name = trim($this->input->post("location-name"));
            $address = trim($this->input->post("location-address"));
            $phone = trim($this->input->post("location-phone"));
            $email = trim($this->input->post("location-email"));
            $long = trim($this->input->post("location-long"));
            $lat = trim($this->input->post("location-lat"));

            $isNewLocation = true;
            $branch = new Branch();
            if ($id != "" && $id != null) {
                $branch = $this->findById("Branch", $id);
                $isNewLocation = false;
            }else{
                if($this->findByName($name,"Branch") != null){
                    throw new RuntimeException("Branch already Exist");
                }
            }
            $branch->setName($name);
            $branch->setDateCreate(new DateTime());
            $branch->setAddress($address);
            $branch->setPhone($phone);
            $branch->setEmail($email);
            $branch->setLongitude($long);
            $branch->setLatitude($lat);
            $this->save($branch);

            //Need to update new Location in Existing Inventory.
            if ($isNewLocation) {
                $products = $this->findAll("Product");
                foreach($products as $product){
                    $stock = new Stock();
                    $stock->setLocation($branch);
                    $stock->setTotalStock(0);
                    $stock->setActualStock(0);

                    $product->addStock($stock);
                    $this->save($product);
                }
            }

            $categories = $this->findAll("Branch");
            $response->setData($categories);
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response);
    }

    function deleteLocation($id = null) {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $response = new Response();
        try {
            throw new RuntimeException("Bloced Location Deleted For now. \nTODO - What if location is deleted and stock exist.");
            if ($id != null && $id != "") {
                $branch = $this->findById("Branch", $id);
                $this->delete($branch);

                $branches = $this->findAll("Branch");
                $response->setData($branches);
            } else {
                throw new Exception("Invalid Branch Id " . $id);
            }
        } catch (Exception $e) {
            $response->setError(true);
            $response->setErrorMessage($e->getMessage());
        }
        header('Content-Type: application/json');
        echo json_encode($response);
    }
} 